package com.nativetalk.controller.app.article;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.article.TsNtArticleInfoIndex;
import com.nativetalk.bean.article.TsNtArticleInfoIndexMap;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ArticleController {
    private static Logger logger = Logger.getLogger(ArticleController.class);

    /**
     * @return RetInfo
     * @Purpose 帮助中心
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/article/helpCenter", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo helpCenter() {
        try {
            //打印入惨
            logger.info(Constants.prefix_in);

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            List<TsNtArticleInfoIndex> helpCenter = (List<TsNtArticleInfoIndex>) MemcachedUtils.get(MemcachedKey.helpCenter);
            map.put("articles", helpCenter);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取帮助中心文章成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 帮助中心(教师版)
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/article/teachHelpCenter", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teachHelpCenter() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            List<TsNtArticleInfoIndex> whelpCenter = (List<TsNtArticleInfoIndex>) MemcachedUtils.get(MemcachedKey.whelpCenter);
            map.put("articles", whelpCenter);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取帮助中心文章成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 文章详情
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/article/detail", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView detail(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("/app/article/detail");
        try {
            //打印入惨
            String arti_id = request.getParameter("arti_id");
            logger.info(Constants.prefix_in + "arti_id=" + arti_id);
            Map<String, TsNtArticleInfoIndexMap> map = (Map<String, TsNtArticleInfoIndexMap>) MemcachedUtils.get(MemcachedKey.tsNtArticleInfoIndexMap);
            TsNtArticleInfoIndexMap tsNtArticleInfoIndexMap = map.get(arti_id);
            mv.addObject("content", tsNtArticleInfoIndexMap.getArti_cont());
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(tsNtArticleInfoIndexMap.getArti_cont()));
        } catch (Exception e) {
            //记录错误日志
            logger.error(e.getMessage(), e);
        }
        return mv;
    }
}


